<?php

namespace App\Http\Controllers\Home;

use App\Model\Cate;
use App\Model\Article;
use App\Model\Collect;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
    //
    public function index()
    {
    	// 获取相关二级分类及分类下的文章
    	$cate_arts = Cate::where('cate_pid','<>',0)->with('article')->get();

    	return view('home.index',compact('cate_arts'));
    }

    public function collect(Request $request)
    {
    	// 获取ajax所提交的数据
    	$uid = $request->input('uid');
    	$artid = $request->input('artid');
    	$action = $request->input('act');

    	// 判断操作状态
    	switch ($action) {
    		case 'add':
    			$collect = Collect::where([
    				['uid','=',$uid],
    				['art_id','=',$artid]
    			])->get();

    			if (!$collect->isEmpty()) {
    				$res = Collect::insert(['uid'=>$uid,'art_id'=>$artid]);
    				Article::where('id',$artid)->increment('art_collect');

    				if ($res) {
    					return response()->json(['status'=>0,'msg'=>'已收藏']);
    				}else{
    					return response()->json(['status'=>1,'msg'=>'收藏失败']);
    				}
    			}else{
    				return response()->json(['status'=>0,'msg'=>'已收藏']);
    			}

    			break;
    		
    		case 'remove':
    			$collect = Collect::where([
    				['uid','=',$uid],
    				['art_id','=',$artid]
    			])->first();

    			if (!empty($collect)) {
    				Article::where('id',$artid)->decrement('art_collect');
    				$res = $collect->delete();

    				if ($res) {
    					return response()->json(['status'=>0,'msg'=>'请收藏']);
    				}else{
    					return response()->json(['status'=>1,'msg'=>'取消收藏失败']);
    				}
    			}else{
    				return response()->json(['status'=>0,'msg'=>'请收藏']);
    			}

    			break;
    	}
    }

    public function lists(Request $request,$id)
    {
        $cate = Cate::find($id);

        $cateid = $cate->id;

        $catename = $cate->cate_name;

        $arr = [];
        if($cate->cate_pid == 0){
            $cate = Cate::where('cate_pid',$cate->id)->get();
            $arr = [];
            foreach ($cate as $v){
                $arr[] = $v->id;
            }
        }else{
            $arr[] = $cate->id;
        }

        $arts = Article::whereIn('cate_id',$arr)->paginate(5);
        return view('home.lists',compact('catename','cateid','arts'));
    }

    public function detail(Request $request,$id)
    {
        \DB::table('article')
            ->where('id', $id)
            ->increment('art_view');

        $art = Article::with('cate')->where('id',$id)->first();
        return view('home.detail',compact('art',$art));

        $pre = Article::where('id','<',$id)->orderBy('id','desc')->first();

        $next = Article::where('id','>',$id)->orderBy('id','asc')->first();

        $similar = Article::where('id',$art->cate_id)->take(4)->get();

        $comment = Comment::where('post_id',$art->id)->get();
        return view('home.detail',compact('art','pre','next','similar','comment'));
    }

    public function comment(Request $request)
    {
        $input = $request->all();
        $res = Comment::create(['nickname'=>$input['author'],'content'=>$input['comment'],'post_id'=>$input['comment_post_ID']]);

        if($res){

            return redirect('detail/'.$input['comment_post_ID']);
        }else{

            return redirect('detail/'.$input['comment_post_ID']);
        }
    }
}
